<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bookmanager.model.Book" %>
<%@ page import="com.bookmanager.model.User" %>
<%
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect("login.jsp");
        return;
    }

    Book book = (Book) request.getAttribute("book");
    if (book == null) {
        response.sendRedirect("books?message=book_not_found");
        return;
    }
%>
<html>
<head>
    <title><%= book.getTitle() %> - 图书详情</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            border-bottom: 1px solid #eee;
            padding-bottom: 15px;
        }
        .book-info {
            display: grid;
            grid-template-columns: 1fr 2fr;
            gap: 30px;
            margin-bottom: 30px;
        }
        .book-cover {
            text-align: center;
            font-size: 8rem;
            color: #667eea;
        }
        .book-details h2 {
            color: #333;
            margin-bottom: 10px;
        }
        .detail-item {
            margin-bottom: 15px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .detail-label {
            font-weight: bold;
            color: #666;
            display: inline-block;
            width: 100px;
        }
        .action-buttons {
            display: flex;
            gap: 15px;
            margin-top: 30px;
        }
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            text-align: center;
            font-size: 14px;
        }
        .btn-borrow {
            background: #28a745;
            color: white;
        }
        .btn-back {
            background: #6c757d;
            color: white;
        }
        .status-available {
            color: #28a745;
            font-weight: bold;
        }
        .status-borrowed {
            color: #dc3545;
            font-weight: bold;
        }
        .btn-favorite {
            background: #e83e8c;
            color: white;
            padding: 5px 10px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            font-size: 0.85rem;
        }

        .btn-favorite:hover {
            background: #d91a72;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>📚 图书详情</h1>
        <div>
            <span>欢迎，<%= user.getUsername() %></span>
            <a href="books" style="margin-left: 15px; color: #007bff;">返回图书列表</a>
        </div>
    </div>

    <div class="book-info">
        <div class="book-cover">📖</div>
        <div class="book-details">
            <h2><%= book.getTitle() %></h2>

            <div class="detail-item">
                <span class="detail-label">ISBN:</span>
                <%= book.getIsbn() != null ? book.getIsbn() : "暂无" %>
            </div>

            <div class="detail-item">
                <span class="detail-label">作者:</span>
                <%= book.getAuthor() != null ? book.getAuthor() : "未知" %>
            </div>

            <div class="detail-item">
                <span class="detail-label">出版社:</span>
                <%= book.getPublisher() != null ? book.getPublisher() : "未知" %>
            </div>

            <div class="detail-item">
                <span class="detail-label">出版日期:</span>
                <%= book.getPublishDate() != null ? book.getPublishDate() : "未知" %>
            </div>

            <div class="detail-item">
                <span class="detail-label">价格:</span>
                ¥<%= book.getPrice() != null ? book.getPrice() : "0.00" %>
            </div>

            <div class="detail-item">
                <span class="detail-label">分类:</span>
                <%= book.getCategory() != null ? book.getCategory() : "未分类" %>
            </div>

            <div class="detail-item">
                <span class="detail-label">位置:</span>
                <%= book.getLocation() != null ? book.getLocation() : "未知" %>
            </div>

            <div class="detail-item">
                <span class="detail-label">可借数量:</span>
                <%= book.getAvailableCopies() %>/<%= book.getTotalCopies() %>
            </div>

            <div class="detail-item">
                <span class="detail-label">状态:</span>
                <span class="status-<%= book.getStatus() %>">
                    <%= "available".equals(book.getStatus()) ? "可借" : "已借出" %>
                </span>
            </div>

            <% if (book.getDescription() != null && !book.getDescription().isEmpty()) { %>
            <div class="detail-item">
                <span class="detail-label">描述:</span><br>
                <p style="margin-top: 10px; line-height: 1.6;"><%= book.getDescription() %></p>
            </div>
            <% } %>
        </div>
    </div>

    <div class="action-buttons">
        <% if ("available".equals(book.getStatus()) && book.getAvailableCopies() > 0) { %>
        <button class="btn btn-borrow" onclick="borrowBook(<%= book.getBookId() %>)">借阅此书</button>
        <% } %>
        <button class="btn btn-favorite" onclick="toggleFavorite(<%= book.getBookId() %>)"
                style="background: #e83e8c; color: white;">
            ⭐ 收藏
        </button>
        <a href="books" class="btn btn-back">返回列表</a>
    </div>
</div>

<script>
    function borrowBook(bookId) {
        if (confirm('确定要借阅《<%= book.getTitle() %>》吗？')) {
            window.location.href = 'borrow?action=borrow&bookId=' + bookId;
        }
    }

    function toggleFavorite(bookId, isCurrentlyFavorite) {
        const action = isCurrentlyFavorite ? 'remove' : 'add';
        const confirmMessage = isCurrentlyFavorite ?
            '确定要取消收藏这本书吗？' : '确定要收藏这本书吗？';

        if (confirm(confirmMessage)) {
            // 创建表单提交收藏请求
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = 'favorite';

            const actionInput = document.createElement('input');
            actionInput.type = 'hidden';
            actionInput.name = 'action';
            actionInput.value = action;
            form.appendChild(actionInput);

            const bookIdInput = document.createElement('input');
            bookIdInput.type = 'hidden';
            bookIdInput.name = 'bookId';
            bookIdInput.value = bookId;
            form.appendChild(bookIdInput);

            document.body.appendChild(form);
            form.submit();
        }
    }
</script>
</body>
</html>
